% The following scripts are used to 
% calculate the amplitude and phase of various 
% tidal modes in a data. The scripts uses the
% tidal processing scripts in /extern/tide/

cd /home/mnair/m/extern/tide/
load /home/mnair/projects/obs/out.txt
load /home/mnair/projects/obs/time.txt
fday = datenum(time(:,1),time(:,2),time(:,3),time(:,4),time(:,5),0);
tidal=tidalfit([fday';out(:,4)']','fm','robust');

%% The script tidalfit does LS fitting to sines and cosines
% of different periodicities. For example for the period 0.5 days
% the time axis (fday) is the decimal date (from datenum)

period = 0.5; %days 

p=polyfit(fday,data,1);
data = data-polyval(p,fday);

pred(:,2) = sin(fday*2*pi/period);
pred(:,1) = cos(fday*2*pi/period);

reg = pred\data; % LS

reg = robustfit(pred,data); % Robust
reg = reg([2:end 1]); %Robust

amp = sqrt(nansum(reg(1:2).^2));
phas = atan2(reg(2),reg(1));

% I found that the LS and robust is giving slightly different
% results. While the robust processing is considered to be better
% than the LS, the results should be verified againist some known
% values

